package org.infrastructure.redis.core;

import java.nio.charset.Charset;

import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;
import org.springframework.util.Assert;

import com.fasterxml.jackson.databind.ObjectMapper;

public class GsonRedisSerializer<T> implements RedisSerializer<T> {
	private static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");

	@SuppressWarnings("unused")
	private ObjectMapper objectMapper = new ObjectMapper();
	private final Class<T> clazz;

	public GsonRedisSerializer(final Class<T> clazz) {
		super();
		this.clazz = clazz;
	}

	@Override
	public byte[] serialize(final T t) throws SerializationException {
		if (t == null) {
			return new byte[0];
		}
		return JosnRedisUtil.toJson(t).getBytes(GsonRedisSerializer.DEFAULT_CHARSET);
	}

	@Override
	public T deserialize(final byte[] bytes) throws SerializationException {
		if (bytes == null || bytes.length <= 0) {
			return null;
		}
		final String str = new String(bytes, GsonRedisSerializer.DEFAULT_CHARSET);

		return JosnRedisUtil.toBean(str, clazz);
	}

	public void setObjectMapper(final ObjectMapper objectMapper) {
		Assert.notNull(objectMapper, "'objectMapper' must not be null");
		this.objectMapper = objectMapper;
	}
}
